package design.pattern.application.specifications;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program JavaBase
 * @description: 规格模式 - 没有使用设计模式，比较蠢的写法
 *  实现sql语法
 * @author: zhanglu
 * @create: 2020-05-24 14:50:00
 */
public class SpecificationsPatten01 {

    public static void main(String[] args) {
        //首先初始化一批用户
        ArrayList<User01> userList = new ArrayList<User01>();
        userList.add(new User01("苏大",3));
        userList.add(new User01("牛二",8));
        userList.add(new User01("张三",10));
        userList.add(new User01("李四",15));
        userList.add(new User01("王五",18));
        userList.add(new User01("赵六",20));
        userList.add(new User01("马七",25));
        userList.add(new User01("杨八",30));
        userList.add(new User01("侯九",35));
        userList.add(new User01("布十",40));
        //定义一个用户查询类
        IUserProvider01 userProvider = new UserProvider01(userList);
        //打印出年龄大于20岁的用户
        System.out.println("===年龄大于20岁的用户===");
        for(User01 u:userProvider.findUserByAgeThan(20)){
            System.out.println(u);
        }

    }

}
//用户类
@Data
class User01{
    private String name;
    private int age;
    public User01(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
//用户操作对象接口
interface IUserProvider01{
    //根据用户用户名查找用户
    List<User01> findUserByNameEq(String name);
    //年龄大于指定年龄的用户
    List<User01> findUserByAgeThan(int age);
}
class UserProvider01 implements IUserProvider01{
    private List<User01> userList = new ArrayList<>();
    public UserProvider01(List<User01> userList){
        this.userList = userList;
    }
    @Override
    public List<User01> findUserByNameEq(String name) {
        return userList.stream().filter(o -> o.getName().equals(name)).collect(Collectors.toList());
    }
    @Override
    public List<User01> findUserByAgeThan(int age) {
        return userList.stream().filter(o -> o.getAge() > age).collect(Collectors.toList());
    }
}